package Program.UserUI;

import Program.Information.User;
import Program.SqlTool.UserDAO;
import Program.Tool.Popup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RepairPaneSon2 extends JPanel {
    private User user;
    //滚动条
    private JScrollPane jScrollPane1, jScrollPane2;
    //日期
    private String date;
    //添加按钮
    private JButton addJButton;
    //清空按钮
    private JButton clearJButton;
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD, 20);
    //报修物品输入框
    private JTextField articleField;
    //住址输入框
    private JTextArea placeArea;
    //报修详细信息输入框
    private JTextArea detailsArea;

    public RepairPaneSon2() {
        super(null);
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
    }

    public RepairPaneSon2(User user, String date) {
        super(null);
        setBounds(0, 0, 900, 600);
        setBackground(Color.WHITE);
        this.user = user;
        this.date = date;
        initField();
        initJButton();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(new ImageIcon("Application_images\\userBG2.png").getImage(), 0, 0, this.getWidth(), this.getHeight(), this);
    }

    private void initField() {
        //日期标签
        JLabel dateJLabel = new JLabel("日  期");
        dateJLabel.setBounds(260, 50, 100, 50);
        dateJLabel.setFont(font);
        this.add(dateJLabel);
        //日期内容标签
        JLabel dateContentJLabel = new JLabel(date, JLabel.CENTER);
        dateContentJLabel.setBounds(400, 50, 120, 50);
        dateContentJLabel.setOpaque(true);
        dateContentJLabel.setBackground(Color.lightGray);
        dateContentJLabel.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        dateContentJLabel.setFont(font);
        this.add(dateContentJLabel);

        //报修物品标签
        JLabel articleJLabel = new JLabel("报修物品");
        articleJLabel.setBounds(250, 120, 100, 50);
        articleJLabel.setFont(font);
        //报修物品输入框
        articleField = new JTextField();
        articleField.setBounds(350, 130, 250, 30);
        articleField.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        articleField.setFont(font);
        this.add(articleJLabel);
        this.add(articleField);

        //住址标签
        JLabel placeJLabel = new JLabel("住址信息");
        placeJLabel.setBounds(250, 175, 100, 50);
        placeJLabel.setFont(font);
        //住址输入框
        placeArea = new JTextArea();
        //多行输入框设置自动换行
        placeArea.setLineWrap(true);
        //多行输入框设置无边框
        placeArea.setBorder(null);
        //设置字体
        placeArea.setFont(font);
        //设置文字提示
        placeArea.setToolTipText("门牌信息格式：xx栋xx单元xx号");
        //滚动条赋予对象
        jScrollPane1 = new JScrollPane(placeArea);
        //设置滚动条水平和垂直的格式
        jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //设置滚动条的位置
        jScrollPane1.setBounds(350, 185, 250, 80);
        jScrollPane1.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        this.add(placeJLabel);
        this.add(jScrollPane1);

        //报修详细信息标签
        JLabel detailsJLabel = new JLabel("详细信息");
        detailsJLabel.setBounds(250, 280, 100, 50);
        detailsJLabel.setFont(font);
        //报修详细信息输入框
        detailsArea = new JTextArea();
        //多行输入框设置自动换行
        detailsArea.setLineWrap(true);
        //多行输入框设置无边框
        detailsArea.setBorder(null);
        //设置字体
        detailsArea.setFont(font);
        //设置文字提示
        detailsArea.setToolTipText("请填写报修详细信息");
        //滚动条赋予对象
        jScrollPane2 = new JScrollPane(detailsArea);
        //设置滚动条水平和垂直的格式
        jScrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        //设置滚动条的位置
        jScrollPane2.setBounds(350, 290, 250, 120);
        jScrollPane2.setBorder(BorderFactory.createLineBorder(Color.black, 2));
        this.add(detailsJLabel);
        this.add(jScrollPane2);
    }

    private void initJButton() {
        //添加按钮
        addJButton = new JButton("添加");
        addJButton.setFont(font);
        addJButton.setBounds(380, 450, 80, 65);
        if (user.getState().equals("0")) {
            // 账号不可用的情况下，无法正常使用添加功能
            addJButton.setBackground(Color.DARK_GRAY);
            addJButton.setEnabled(false);
            addJButton.setToolTipText("您的账户不可用,请联系管理员");
        } else {
            addJButton.setBackground(new Color(235, 135, 112));
            addJButton.setEnabled(true);
        }
        //取消按钮的焦点框
        addJButton.setFocusPainted(false);
        //取消按钮的边框
        addJButton.setBorder(null);
        //设置鼠标样式
        addJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //清空按钮
        clearJButton = new JButton("清空");
        clearJButton.setFont(font);
        clearJButton.setBounds(500, 450, 80, 65);
        clearJButton.setBackground(new Color(191, 230, 240));
        //取消按钮的焦点框
        clearJButton.setFocusPainted(false);
        //取消按钮的边框
        clearJButton.setBorder(null);
        //设置鼠标样式
        clearJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //获取监听器
        ActionListener listener = getListener();
        //按钮添加监听
        addJButton.addActionListener(listener);
        clearJButton.addActionListener(listener);

        //添加按钮
        this.add(addJButton);
        this.add(clearJButton);

    }

    private ActionListener getListener() {
        ActionListener listener = new ActionListener() {
            //弹窗信息
            String info = "";

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == addJButton) {
                    //获取报修物品名
                    String article = articleField.getText();
                    //获取住址
                    String place = placeArea.getText();
                    //获取报修详细信息
                    String details = detailsArea.getText();
                    if (article.length() * place.length() * details.length() == 0) {
                        info = "请完善信息";
                        Popup.showJDialog(info);
                    } else if (!PFCheck(place)) {
                        info = "<html>门牌信息格式：xx栋xx单元xx号</html>";
                        Popup.showJDialog(info);
                        placeArea.setText("");
                    } else {
                        boolean b = UserDAO.addRepair(user.getUserID(), article, place, date, details);
                        if (b) {
                            info = "添加成功";
                            Popup.showJDialog(info);
                            clearField();
                        } else {
                            info = "添加失败";
                            Popup.showJDialog(info);
                            clearField();
                        }
                    }
                } else if (e.getSource() == clearJButton) {
                    clearField();
                }
            }
        };
        return listener;
    }

    //清除所有输入框方法
    private void clearField() {
        articleField.setText("");
        placeArea.setText("");
        detailsArea.setText("");
    }

    //门牌信息格式检查
    private boolean PFCheck(String inputPlace) {
        //xx小区xx栋xx单元xx号
        String regex = "^\\d{1,2}栋\\d{1,2}单元\\d{1,4}号$";
        boolean flag = inputPlace.matches(regex);
        return flag;
    }

}
